package com.plugin.uniapp.dialog;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.plugin.uniapp.MainActivity;
import com.plugin.uniapp.R;

import org.json.JSONException;
import org.json.JSONObject;

public class AccountView extends BaseView {

    private boolean isPhoneMode = true;
    public static String inputPhoneOrEmail;

    public AccountView(MultiViewDialog dialog) {
        super(dialog, R.layout.view_account);

        // 返回按钮
        layout.findViewById(R.id.btn_back).setOnClickListener(v -> {
            dialog.switch2VideoPanel();
        });

        // 帮助按钮
        layout.findViewById(R.id.btn_help).setOnClickListener(v -> {
            // 帮助逻辑
        });

        // 标签切换

        layout.findViewById(R.id.tab_left).setOnClickListener(v -> {
            switchToPhone();
        });

        layout.findViewById(R.id.tab_right).setOnClickListener(v -> {
            switchToEmail();
        });

        // 下一步按钮
        layout.findViewById(R.id.btn_next).setOnClickListener(v -> {
            layout.findViewById(R.id.txt_error).setVisibility(View.GONE);
            //发送请求：
            dialog.showLoading();
            try {
                JSONObject json = new JSONObject();
                json.put("cmd", "login_req");
                JSONObject data = new JSONObject();
                if (isPhoneMode) {
                    inputPhoneOrEmail = ((TextView) layout.findViewById(R.id.input_phone)).getText().toString();
                    data.put("phone", inputPhoneOrEmail);
                    data.put("email", "");
                } else {
                    inputPhoneOrEmail = ((TextView) layout.findViewById(R.id.input_email)).getText().toString();
                    data.put("phone", "");
                    data.put("email", inputPhoneOrEmail);
                }
                json.put("data", data);
                dialog.isWaitingLogin = true;
                dialog.reqWebSocket(json, "login_rsp", (JSONObject rspData, boolean isTimeout) -> {
                    if (!isTimeout) {
                        try {
                            if (rspData.getInt("code") != 0) {
                                TextView txtError = layout.findViewById(R.id.txt_error);
                                txtError.setVisibility(View.VISIBLE);
                                txtError.setText(rspData.getString("msg"));
                            } else {
                                dialog.switch2CaptchaPanel();
                            }
                        } catch (JSONException e) {
                            error(e.toString());
                        }
                    }
                }, 0);
            } catch (Exception e) {
                error(e.toString());
            }
        });

        ((CheckBox) layout.findViewById(R.id.cb_agree)).setOnCheckedChangeListener((buttonView, isChecked) -> {
            checkConditions();
        });

        ((EditText) layout.findViewById(R.id.input_email)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkConditions();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        ((EditText) layout.findViewById(R.id.input_phone)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkConditions();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        switchToPhone();
        setBtnNextEnable(false);
    }

    private void switchToPhone() {
        isPhoneMode = true;
        View lineLeft = layout.findViewById(R.id.underline_left);
        View lineRight = layout.findViewById(R.id.underline_right);
        lineLeft.setBackgroundColor(Color.parseColor("#666666"));
        lineRight.setBackgroundColor(Color.parseColor("#dddddd"));
        layout.findViewById(R.id.input_email).setVisibility(View.GONE);
        layout.findViewById(R.id.btn_input_clear).setVisibility(View.GONE);
        layout.findViewById(R.id.phone_layout).setVisibility(View.VISIBLE);
        checkConditions();
    }

    private void switchToEmail() {
        isPhoneMode = false;
        View lineLeft = layout.findViewById(R.id.underline_left);
        View lineRight = layout.findViewById(R.id.underline_right);
        lineLeft.setBackgroundColor(Color.parseColor("#dddddd"));
        lineRight.setBackgroundColor(Color.parseColor("#666666"));
        layout.findViewById(R.id.input_email).setVisibility(View.VISIBLE);
        layout.findViewById(R.id.btn_input_clear).setVisibility(View.GONE);
        layout.findViewById(R.id.phone_layout).setVisibility(View.GONE);
        checkConditions();
    }

    private void checkConditions() {
        boolean isChecked = ((CheckBox) layout.findViewById(R.id.cb_agree)).isChecked();
        boolean hasText;
        if (isPhoneMode) {
            hasText = !((EditText) layout.findViewById(R.id.input_phone)).getText().toString().trim().isEmpty();
        } else {
            hasText = !((EditText) layout.findViewById(R.id.input_email)).getText().toString().trim().isEmpty();
        }
        setBtnNextEnable(isChecked && hasText);
    }

    private void setBtnNextEnable(boolean enable) {
        Button btn = layout.findViewById(R.id.btn_next);
        btn.setEnabled(enable);
        btn.setAlpha(enable ? 1 : 0.3f);
    }
}
